package com.huitongmaoyi.ai;

import com.huitongmaoyi.ai.dao.CustomerDAO;
import com.huitongmaoyi.ai.entity.Customer;
import com.huitongmaoyi.ai.service.AIAnalysisService;
import com.huitongmaoyi.ai.service.AuthService;
import com.huitongmaoyi.ai.service.ChatService;
import com.huitongmaoyi.ai.service.CustomerService;

import java.io.PrintWriter;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        AuthService auth = new AuthService();
        // 初始化输出流（复用）
        PrintWriter out = new PrintWriter(System.out, true);
        // 初始化AI分析服务（传入输入输出流）
        AIAnalysisService aiAnalysisService = new AIAnalysisService(scanner, out);
        String currentUsername = null;

        System.out.println("=== 欢迎使用慧通贸易 AI 系统 ===");

        // 登录/注册循环（保持不变）
        while (currentUsername == null) {
            System.out.println("\n请选择操作：");
            System.out.println("1. 登录");
            System.out.println("2. 注册新账号");
            System.out.println("3. 退出");
            System.out.print("请输入选项（1/2/3）: ");

            String choice = scanner.nextLine().trim();
            switch (choice) {
                case "1":
                    currentUsername = handleLogin(scanner, auth);
                    break;
                case "2":
                    handleRegister(scanner, auth);
                    break;
                case "3":
                    System.out.println("再见！");
                    scanner.close();
                    return;
                default:
                    System.out.println("❌ 无效选项，请输入 1、2 或 3。");
            }
        }

        // 登录成功后主菜单
        System.out.println("\n🎉 欢迎进入系统，" + currentUsername + "！");
        while (true) {
            System.out.println("\n请选择操作：");
            System.out.println("1. 查看所有客户");
            System.out.println("2. 使用 AI 智能匹配客户");
            System.out.println("3. 使用 AI 智能分析（自由对话）");
            System.out.println("4. 聊天系统");
            System.out.println("5. 退出");
            System.out.print("请输入选项：");

            String choice = scanner.nextLine().trim();
            if ("1".equals(choice)) {
                // 查看客户列表
                CustomerDAO dao = new CustomerDAO();
                List<Customer> customers = dao.getAllCustomers();
                if (customers.isEmpty()) {
                    System.out.println("⚠️  暂无客户数据。请先插入测试数据到 `customers` 表。");
                } else {
                    System.out.println("\n📋 客户列表（共 " + customers.size() + " 条）：");
                    System.out.println("-".repeat(110));
                    customers.forEach(System.out::println);
                    System.out.println("-".repeat(110));
                }

            } else if ("2".equals(choice)) {
                // 智能匹配客户
                new CustomerService(out).startConversation(scanner);
            } else if ("3".equals(choice)) {
                // AI智能分析
                aiAnalysisService.startConversation();
            } else if ("4".equals(choice)) {
                // 修复：传入PrintWriter参数（使用系统输出流）
                new ChatService(scanner, currentUsername, out).startChat();
            } else if ("5".equals(choice)) {
                // 退出系统
                break;
            } else {
                System.out.println("❌ 无效选项，请重新输入！");
            }
        }

        scanner.close();
        System.out.println("再见！");
    }

    // 登录功能
    private static String handleLogin(Scanner scanner, AuthService auth) {
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine().trim();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine().trim();

        if (auth.login(username, password)) {
            return username;
        } else {
            System.out.println("❌ 登录失败！用户名或密码错误。");
            return null;
        }
    }

    // 注册功能
    private static void handleRegister(Scanner scanner, AuthService auth) {
        System.out.print("设置用户名: ");
        String username = scanner.nextLine().trim();
        System.out.print("设置密码: ");
        String password = scanner.nextLine().trim();
        auth.register(username, password);
    }
}